package com.jic.member.service.eventHandler;

import com.google.common.collect.Maps;
import lombok.Setter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * description: EnventHandlerFactory
 * date: 2020/6/8 11:26
 * author: wangys
 * version: 1.0
 */
@Component
public class EventHandlerFactory implements InitializingBean, ApplicationContextAware {
    @Setter
    private ApplicationContext applicationContext;

    private Map<String, EventHander> handlerHashMap = Maps.newHashMap();

    @Override
    public void afterPropertiesSet() throws Exception {
        applicationContext.getBeansOfType(EventHander.class)
            .forEach((k, v) -> {
                handlerHashMap.put(v.getEventCode(), v);
            });
    }

    public EventHander getEventHanderByEventCode(String eventCode) {
        return handlerHashMap.get(eventCode);
    }
}
